package cn.calm.osgi.admin.bundle.util;

import java.io.File;
import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipException;

import cn.calm.osgi.admin.bundle.entity.BundleInfo;

public class BundleUtil {
	public static BundleInfo readBundleInfo(File jarFile) throws Exception {
		BundleInfo bundle=null;
		try {
			JarFile file = new JarFile(jarFile);
			Manifest manifest = file.getManifest();
			Attributes attributes = manifest.getMainAttributes();
			String symbolicName = attributes.getValue("Bundle-SymbolicName");
			if (symbolicName == null) {
				throw new Exception();
			}
			String bundleVersion = attributes.getValue("Bundle-Version");
			if (bundleVersion == null) {
				throw new Exception();
			}
			bundle = new BundleInfo();
			bundle.setBundleFilename(jarFile.getName());
			bundle.setSymbolicName(symbolicName);
			bundle.setVersion(bundleVersion);
		} catch (ZipException e) {
			throw new Exception("该文件不是个Bundle");
		} catch (IOException e) {
			throw new Exception("该文件不是个Bundle");
		} catch (Exception e) {
			throw new Exception("该文件不是个Bundle");
		}
		return bundle;
	}
}
